var MessageShow = function(msg){
	//提示信息
	alert(msg);
};

var BindImgEvent = function(){
	$(".Upload-img .textarea-div .button-save").bind({
		click:function(e){
			var b = e.target;
			var css = {
				"display":"none"
			};
			var css2= {
				"display":"block"
			};
			$(b).parent().css(css);
			$(b).parent().parent().children("p").css(css2).html($(b).parent().children("textarea").val());
		}
	});
	
	
	$(".Upload-img .textarea-div .button-cancel").bind({
		click:function(e){
			var b = e.target;
			var css = {
				"display":"none"
			};
			var css2= {
				"display":"block"
			};
			$(b).parent().css(css);
			$(b).parent().parent().children("p").css(css2);
	
		}
	});
	
	$(".Upload-img p[name=descrip]").bind({
		mouseover:function(e){
			var p = e.target;
			var css = {
				"backgroundColor":"#00ca00",
				"color":"#ffffff"
			};
			$(p).css(css);
		},
		mouseout:function(e){
			var p = e.target;
			var css = {
				"backgroundColor":"#ffffff",
				"color":"#00ca00"
			};
			$(p).css(css);
		},
		click:function(e){
			var p = e.target;
			var css = {
				"display":"none"
			};
			var css2 = {
				"display":"block"
			}
			$(p).css(css);
			$(p).next().css(css2);
			$(p).next().children("textarea").val($(p).text()).focus();
		}
	});
};





jQuery.extend({
	/** * @see 将json字符串转换为对象 * @param json字符串 * @return 返回object,array,string等对象 */
	evalJSON : function(strJson) {
		return eval("(" + strJson + ")");
	}
});

jQuery.extend({
	/** * @see 将javascript数据类型转换为json字符串 * @param 待转换对象,支持object,array,string,function,number,boolean,regexp * @return 返回json字符串 */
	toJSON : function(object) {

		var type = typeof object;
		if ('object' == type) {
			if (Array == object.constructor)
				type = 'array';
			else if (RegExp == object.constructor)
				type = 'regexp';
			else
				type = 'object';
		}
		switch (type) {
			case 'undefined':
			case 'unknown':
				return;
				break;
			case 'function':
			case 'boolean':
			case 'regexp':
				return object.toString();
				break;
			case 'number':
				return isFinite(object) ? object.toString() : 'null';
				break;
			case 'string':
				return '"' + object.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g, function() {
					var a = arguments[0];
					return (a == '\n') ? '\\n' : (a == '\r') ? '\\r' : (a == '\t') ? '\\t' : ""
				}) + '"';
				break;
			case 'object':
				if (object === null)
					return 'null';
				var results = [];
				for (var property in object) {
					var value = jQuery.toJSON(object[property]);
					if (value !== undefined)
						results.push(jQuery.toJSON(property) + ':' + value);
				}
				return '{' + results.join(',') + '}';
				break;
			case 'array':
				var results = [];
				for (var i = 0; i < object.length; i++) {
					var value = jQuery.toJSON(object[i]);
					if (value !== undefined)
						results.push(value);
				}
				return '[' + results.join(',') + ']';
				break;
		}
	}
});

var bindItemClick = function(){
		$(".dropdown-menu li").bind({
		click:function(e){
			var b = e.target;
			$(b).parent().parent().parent().children(".dropdown-toggle").html($(b).html()+"<b class='caret'></b>");
			$(b).parent().parent().parent().children(".dropdown-toggle").attr("tagid",$(b).parent().attr("tagid"));
		}
			
	});
};


$("#issue-button").bind({
	click:function(e){
		var dict = $(".content_d_list");
		var dict_num = dict.size();
		
		//循环判断type不能为空
		for(var i = 0; i < dict_num; i++){
			var parm = {};
			parm.PID = $(dict[i]).attr("pid");
			parm.type_cont = $(dict[i]).find("#dropdown-cont .dropdown-toggle").attr("tagid");
			parm.type_style = $(dict[i]).find("#dropdown-style .dropdown-toggle").attr("tagid");
			if(parm.type_cont == undefined && parm.type_style == undefined){
				//提示不能为空
				MessageShow("风格信息不能为空");
				return;
			}
		}
		
		//循环获取数据
		
		for(var i = 0; i < dict_num; i++){
			var parm = {};
			parm.uid = "1";  //测试用 用户ID
			parm.PID = $(dict[i]).attr("pid");
			parm.type_cont = $(dict[i]).find("#dropdown-cont .dropdown-toggle").attr("tagid");
			parm.type_style = $(dict[i]).find("#dropdown-style .dropdown-toggle").attr("tagid");
			parm.position = $(dict[i]).find("#text-position").val();
			parm.descrip = $(dict[i]).find('#text-descrip').val();
			$.ajax({
                url: "AJAX/Upload.ashx?op=IssueSingle",
                type: 'POST',
                //data:$.toJSON(parm),
                data:parm,
                dataType: "json",
                success: function (json) {
                    if (json.success === "true") {
                    	//成功并且跳转
                        
                        MessageShow("发布成功");
                        window.location.href="Default.aspx";
                    }
                    else {
                        
                        MessageShow("第"+ (i+1) +"张图片发布失败");
                    }

                }
           });
			
			
		}
	}
});

var BindBoard = {
	TypeCont:function(json,target){
	    var b = $("#dropdown-cont .dropdown-menu:empty");
		for(var e in json.data){
			var text = "<li tagid ='"+json.data[e].ID+"' >";
			text +="<a>"+json.data[e].TagName +"</a></li>"
			//插入到Html中
			b.append(text);
		};
	},
	TypeStyle:function(json,target){
	    var b = $("#dropdown-style .dropdown-menu:empty");
		for(var e in json.data){
			var text = "<li tagid ='"+json.data[e].ID+"' >";
			text +="<a>"+json.data[e].TagName +"</a></li>"
			//插入到Html中
			b.append(text);
		};
	}
};

var BindTypeData =  function() {
  	$.ajax({
		 url: "AJAX/GetUploadInfo.ashx?op=getTpyeCont",
         type: 'GET',
         data:"",
         dataType: "json",
         success:function(json){
         	if(json.success === "true"){
         		BindBoard.TypeCont(json);
         		bindItemClick();
         	}
         	else{
         		//弹框
         		MessageShow("获取类型信息失败，请刷新重试");
         	}
         }
	});
	
	//绑定类型 风格
	$.ajax({
		 url: "AJAX/GetUploadInfo.ashx?op=getTpyeStyle",
         type: 'GET',
         data:"",
         dataType: "json",
         success:function(json){
         	if(json.success === "true"){
         		BindBoard.TypeStyle(json);
         		bindItemClick();
         	}
         	else{
         		//弹框
         		MessageShow("获取类型信息失败，请刷新重试");
         	}
         }
	});
};

(function($){
	//绑定类型 内容
	$.ajax({
		 url: "AJAX/GetUploadInfo.ashx?op=getTpyeCont",
         type: 'GET',
         data:"",
         dataType: "json",
         success:function(json){
         	if(json.success === "true"){
         		BindBoard.TypeCont(json);
         		bindItemClick();
         	}
         	else{
         		//弹框
         		MessageShow("获取类型信息失败，请刷新重试");
         	}
         }
	});
	
	//绑定类型 风格
	$.ajax({
		 url: "AJAX/GetUploadInfo.ashx?op=getTpyeStyle",
         type: 'GET',
         data:"",
         dataType: "json",
         success:function(json){
         	if(json.success === "true"){
         		BindBoard.TypeStyle(json);
         		bindItemClick();
         	}
         	else{
         		//弹框
         		MessageShow("获取类型信息失败，请刷新重试");
         	}
         }
	});
	

})(jQuery)
